/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hal_test_entry.h"
#include "sle_loader_hal.h"
#include "sle_announce_hal.h"
#include "sle_hal_errcode.h"

static SleAnnounceHalCallbacks *g_sleAnnounceHalUtCbs = NULL;

void OnSleUtAnnounceHalStartAnnounceCallback(uint8_t announceId)
{
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] OnSleUtAnnounceHalStartAnnounceCallback(%d)", announceId);
    return;
}

void OnSleUtAnnounceHalStopAnnounceCallback(uint8_t announceId)
{
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] OnSleUtAnnounceHalStopAnnounceCallback(%d)", announceId);
    return;
}

void OnSleUtAnnounceHalTerminalAnnounceCallbcak(uint8_t announceId)
{
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] OnSleUtAnnounceHalTerminalAnnounceCallbcak(%d)", announceId);
    return;
}

void InitSleAnnounceHalTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] InitSleAnnounceHalTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleAnnounceHalTestLevel0 pass");
}

void DeinitSleAnnounceHalTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = DeinitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] DeinitSleAnnounceHalTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleAnnounceHalTestLevel0 pass");
}

static uint8_t g_announceId;
static SleAnnounceData g_data = {
    .announceLength = 10,
    .announceData = "1234567890"
};
void SleAnnounceHalSetAnnounceDataTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleAnnounceHalSetAnnounceData(g_announceId, &g_data);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceDataTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceDataTestLevel0 pass");
}

static SleAnnounceParam g_param = {
    .handle = 1,
    .mode = SLE_ANNOUNCE_CONNECTABLE_SCANABLE_MODE,
    .role = SLE_ANNOUNCE_T_ROAL_CAN_NEGO,
    .level = SLE_ANNOUNCE_NORMAL_LEVEL,
    .channelMap = SLE_ANNOUNCE_DEFAULT_ALL_CHANEL,
    .annonceIntervalMin = 0xc8,
    .annonceIntervalMax = 0xc8,
    .connectIntervalMin = 0x1E,
    .connectIntervalMax = 0x1E,
    .connectLatency = 0x00,
    .connectTimeout = 0x1F4,
    .ownAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    },
};
void SleAnnounceHalSetAnnounceParamTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleAnnounceHalSetAnnounceParam(g_announceId, &g_param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceParamTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceParamTestLevel0 pass");
}

void SleAnnounceHalStartAnnounceTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleAnnounceHalStartAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalStartAnnounceTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalStartAnnounceTestLevel0 pass");
}

void SleAnnounceHalStopAnnounceTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleAnnounceHalStopAnnounce(g_announceId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalStopAnnounceTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalStopAnnounceTestLevel0 pass");
}

void SleAnnounceHalRegisterHostCallbackTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleAnnounceHalRegisterCallbacks(g_sleAnnounceHalUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalRegisterHostCallbackTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalRegisterHostCallbackTestLevel0 pass");
}

void SleAnnounceHalTestLevel0(void)
{
    SLE_UT_LOG("___SleAnnounceHalTestLevel0 start");
    InitSleAnnounceHalTestLevel0();
    SleAnnounceHalRegisterHostCallbackTestLevel0();
    SleAnnounceHalSetAnnounceDataTestLevel0();
    SleAnnounceHalSetAnnounceParamTestLevel0();
    SleAnnounceHalStartAnnounceTestLevel0();
    SleAnnounceHalStopAnnounceTestLevel0();
    DeinitSleAnnounceHalTestLevel0();
    SLE_UT_LOG("___SleAnnounceHalTestLevel0 end");
}

void InitSleAnnounceHalTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] InitSleAnnounceHalTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleAnnounceHalTestLevel1 pass1");
    //测试重复调用
    ret = InitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] InitSleAnnounceHalTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleAnnounceHalTestLevel1 pass2");
}

void DeinitSleAnnounceHalTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = DeinitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] DeinitSleAnnounceHalTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleAnnounceHalTestLevel1 pass1");
    //测试重复调用
    ret = DeinitSleAnnounceHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] DeinitSleAnnounceHalTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleAnnounceHalTestLevel1 pass2");
}

void SleAnnounceHalSetAnnounceDataTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    // 测试data为NULL
    ret = SleAnnounceHalSetAnnounceData(g_announceId, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_ERR_INVALID_PARAM, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceDataTestLevel1 failed1(%d)", ret);\
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceDataTestLevel1 pass1");
    //测试重复调用
    ret = SleAnnounceHalSetAnnounceData(g_announceId, &g_data);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceDataTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceDataTestLevel1 pass2");
    sleep(1);
    ret = SleAnnounceHalSetAnnounceData(g_announceId, &g_data);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceDataTestLevel1 failed3(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceDataTestLevel1 pass3");
}

void SleAnnounceHalSetAnnounceParamTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    // 测试param为NULL
    ret = SleAnnounceHalSetAnnounceParam(g_announceId, NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_ERR_INVALID_PARAM, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceParamTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceParamTestLevel1 pass1");
    //测试重复调用
    ret = SleAnnounceHalSetAnnounceParam(g_announceId, &g_param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceParamTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceParamTestLevel1 pass2");
    sleep(1);
    ret = SleAnnounceHalSetAnnounceParam(g_announceId, &g_param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalSetAnnounceParamTestLevel1 failed3(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalSetAnnounceParamTestLevel1 pass3");
}

void SleAnnounceHalStartAnnounceTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    // TODO:测试announceId为未注册的Id
    ret = SleAnnounceHalStartAnnounce(g_announceId + 1);
    // SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalStartAnnounceTestLevel1 failed(%d)", ret);
    SleAnnounceHalStartAnnounceTestLevel0();
}

void SleAnnounceHalStopAnnounceTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    // TODO:测试announceId为未注册的Id
    ret = SleAnnounceHalStopAnnounce(g_announceId + 1);
    // SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalStopAnnounceTestLevel1 failed(%d)", ret);
    SleAnnounceHalStopAnnounceTestLevel0();
}

void SleAnnounceHalRegisterHostCallbackTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    //测试入参为NULL
    ret = SleAnnounceHalRegisterCallbacks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_ERR_INVALID_PARAM, "[SleAnnounceHalTest][FAILED] SleAnnounceHalRegisterHostCallbackTestLevel1 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalRegisterHostCallbackTestLevel1 pass1");
    ret = SleAnnounceHalRegisterCallbacks(g_sleAnnounceHalUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleAnnounceHalTest][FAILED] SleAnnounceHalRegisterHostCallbackTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleAnnounceHalRegisterHostCallbackTestLevel1 pass2");
}

void SleAnnounceHalTestLevel1(void)
{
    SLE_UT_LOG("___SleAnnounceHalTestLevel1 start");
    InitSleAnnounceHalTestLevel1();
    SleAnnounceHalRegisterHostCallbackTestLevel1();
    SleAnnounceHalSetAnnounceDataTestLevel1();
    SleAnnounceHalSetAnnounceParamTestLevel1();
    SleAnnounceHalStartAnnounceTestLevel1();
    SleAnnounceHalStopAnnounceTestLevel1();
    DeinitSleAnnounceHalTestLevel1();
    SLE_UT_LOG("___SleAnnounceHalTestLevel1 end");
}

SleHalErrorCode InitSleAnnounceHalTestEntry(void)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_sleAnnounceHalUtCbs == NULL, SLE_HAL_ERR_COMMON,"[SleSeekHalTest][FAILED] g_sleSeekHalCbs is not NULL");
    g_sleAnnounceHalUtCbs = (SleAnnounceHalCallbacks *)malloc(sizeof(SleAnnounceHalCallbacks));
    g_sleAnnounceHalUtCbs->OnAnnounceHalStartAnnounceCb = OnSleUtAnnounceHalStartAnnounceCallback;
    g_sleAnnounceHalUtCbs->OnAnnounceHalStopAnnounceCb = OnSleUtAnnounceHalStopAnnounceCallback;
    g_sleAnnounceHalUtCbs->OnAnnounceHalTerminalAnnounceCb = OnSleUtAnnounceHalTerminalAnnounceCallbcak;
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_sleAnnounceHalUtCbs != NULL, SLE_HAL_ERR_DRIVER_OR_SYSTEM, "[SleAnnounceHalTest][FAILED] malloc g_sleHostHalUtCbs failed");
    return SLE_HAL_SUCCESS;
}

SleHalErrorCode DeinitSleAnnounceHalTestEntry(void)
{
    if (g_sleAnnounceHalUtCbs != NULL) {
        free(g_sleAnnounceHalUtCbs);
        g_sleAnnounceHalUtCbs = NULL;
    }
    return SLE_HAL_SUCCESS;
}

void SleAnnounceHalTestEntry(void)
{
    SLE_UT_LOG("__SleAnnounceHalTestEntry start");
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleAnnounceHalTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SleAnnounceHalTestLevel0();
    SleAnnounceHalTestLevel1();
    ret = DeinitSleAnnounceHalTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SLE_UT_LOG("__SleAnnounceHalTestEntry end");
}